// pages/partnenrinfo/partnerinfo.js
var app = getApp()
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toast1Hidden: true,
    modalHidden: true,
    modalHidden2: true,
    notice_str: '',

    deptName: '',
    username: '',
    phone: '',
    address: '',
    note: '',
    deviceNo:'',

    id: undefined,

  },



  //表单提交
  formSubmit: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认保存信息',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        if (res.confirm) {
          console.log('for发生了submit事件，携带数据为：', e.detail.value);

          var reg = /^[0-9a-zA-Z]+$/;
          var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;

          if (e.detail.value.deviceName.length == 0) {
            wx.showToast({
              title: "设备名称不能为空！",
              icon: 'none',
              duration: 2000
            });
            return false;
          } 
          that.setData({
            deviceNo: e.detail.value.deviceNo,
          })

          wx.request({
            url: api.DeviceInfoUpd,
            data: {
              deviceName: e.detail.value.deviceName,
              deviceNo: e.detail.value.deviceNo,
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: (res) => {
              wx.showLoading({
                title: '加载中',
              })
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateBack({})
                    }
                  }
                });
                wx.hideLoading();
              }
            }
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.hideLoading();
        }
      }
    })
  },

  del: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认删除该设备?',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        if (res.confirm) {
          wx.request({
            url: api.DeviceDel,
            data: {
              deviceNo: that.data.deviceNo,
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: (res) => {
              wx.showLoading({
                title: '加载中',
              })
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateBack({})
                    }
                  }
                });
                wx.hideLoading();
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                });
                wx.hideLoading();
              }
            },
          })

        } else if (res.cancel) {
          console.log('用户点击取消');
          wx.hideLoading();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.setData({
      id: options.id,
    });
    var ID = that.data.id;

    wx.request({
      url: api.DeviceInfoSear,
      data: {
        id: ID,
      },
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        if (res.data.success) {
          var st = "";
          if (res.data.obj.status == '1'){
            st = "已开启"
          } else if (res.data.obj.status == '0'){
            st = "已关闭"
          }
          that.setData({
            deviceName: res.data.obj.deviceName,
            deviceNo: res.data.obj.deviceNo,
            createTime: res.data.obj.createTime,
            status: st,
          });
          wx.hideLoading();
        }
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
